<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="酒店名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入酒店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星级" prop="starLevel">
        <el-select v-model="queryParams.starLevel" placeholder="请选择酒店星级" clearable @change="handleQuery">
          <el-option label="一星" value="1" />
          <el-option label="二星" value="2" />
          <el-option label="三星" value="3" />
          <el-option label="四星" value="4" />
          <el-option label="五星" value="5" />
          <el-option label="六星" value="6" />
          <el-option label="七星" value="7" />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="regionId">
        <el-cascader
          v-model="queryParams.regionId"
          :options="regionsList"
          :props="{ label:'text',checkStrictly:true}"
          clearable
          @change="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="电话" prop="contact">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.contact"-->
      <!--          placeholder="请输入电话"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="标签" prop="label">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.label"-->
      <!--          placeholder="请输入标签"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable @change="handleQuery">
          <el-option label="未上架" value="0" />
          <el-option label="已上架" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="Edit"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="Delete"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading" :data="hotelList" row-key="id" :expand-row-keys="[]" @selection-change="handleSelectionChange"
      @expand-change="handleExpandChange"
    >
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column type="expand" width="30">
        <template #default="props">
          <el-row justify="center">
            <el-table v-loading="props.row.loading" :data="props.row.rooms" style="width: 95%">
              <el-table-column label="房型图片" align="center" prop="photo" width="100">
                <template #default="scope">
                  <el-image style="width: 40px;height: 40px" :src="scope.row.photo.split(',')[0]" :preview-src-list="scope.row.photo.split(',')" />
                </template>
              </el-table-column>
              <el-table-column label="房型名称" min-width="100" align="center" :show-overflow-tooltip="true" prop="roomType" />
              <el-table-column label="床型" align="center" prop="bedType" :show-overflow-tooltip="true" />
              <el-table-column label="入住人数" align="center" prop="occupantsNum" />
              <el-table-column label="是否含早餐" align="center" prop="breakfastFlag">
                <template #default="scope">
                  {{ scope.row.breakfastFlag=='1'?'是':'否' }}
                </template>
              </el-table-column>
              <el-table-column label="早餐数" align="center" prop="breakfastNum">
                <template #default="scope">
                  {{ scope.row.breakfastFlag=='1'?scope.row.breakfastNum:'--' }}
                </template>
              </el-table-column>
              <el-table-column label="是否有窗户" align="center" prop="windowFlag">
                <template #default="scope">
                  {{ scope.row.windowFlag=='1'?'是':'否' }}
                </template>
              </el-table-column>
              <el-table-column label="是否有独立卫浴" align="center" prop="bathroomFlag">
                <template #default="scope">
                  {{ scope.row.bathroomFlag=='1'?'是':'否' }}
                </template>
              </el-table-column>
              <el-table-column label="是否可加床" align="center" prop="addBedFlag">
                <template #default="scope">
                  {{ scope.row.addBedFlag=='1'?'是':'否' }}
                </template>
              </el-table-column>
              <el-table-column label="面积（m²）" align="center" prop="area" />
              <el-table-column label="操作" fixed="right" width="150" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                  <el-button type="text" @click="handleUpdateRoom(scope.row)">编辑</el-button>
                  <el-button type="text" @click="handleDeleteRoom(props.row,scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" width="55" label="序号" align="center" />
      <el-table-column label="酒店图片" width="100" align="center" prop="photos">
        <template #default="scope">
          <el-image style="width: 40px;height: 40px" :src="scope.row.photos.split(',')[0]" :preview-src-list="scope.row.photos.split(',')" />
        </template>
      </el-table-column>
      <el-table-column label="酒店名称" align="center" prop="name" :show-overflow-tooltip="true" min-width="150" />
      <el-table-column label="酒店地址" align="center" prop="addName" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="酒店星级" align="center" prop="starLevel" width="100">
        <template #default="scope">
          {{ starLevelArr[scope.row.starLevel] }}
        </template>
      </el-table-column>
      <el-table-column label="入住时间" align="center" width="100" prop="checkInTime" />
      <el-table-column label="离店时间" align="center" width="100" prop="checkOutTime" />
      <el-table-column label="电话" align="center" prop="contact" width="120" />
      <el-table-column label="标签" align="center" prop="label" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template #default="scope">
          {{ scope.row.status=='0'?'未上架':'已上架' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="220" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status=='1'" type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleStatus(scope.row)">上架</el-button>
          <el-button v-if="scope.row.status=='1'" type="text" @click="handleStatus(scope.row)">下架</el-button>
          <el-button type="text" @click="handleAddRoom(scope.row)">添加房型</el-button>
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <modify-hotel ref="modifyHotelRef" @submit="getList" />
    <modify-room ref="modifyHotelRoomRef" @submit="getRoomList" />
  </div>
</template>

<script setup name="Logistics_Hotel">
import { listHotel, getHotel, delHotel, changeStatus } from '@/api/logex/hotel'
import { reactive } from '@vue/runtime-core'
import { getCurrentInstance, ref, toRefs } from 'vue'
import ModifyHotel from './modify'
import ModifyRoom from './modifyRoom'
import { delHotelRoom, getHotelRoom, listHotelRoom } from '@/api/logex/hotelRoom'
import _ from 'lodash'
import useUserStore from '@/store/modules/user'
import { computed } from '@vue/reactivity'

const { proxy } = getCurrentInstance()

const hotelList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    starLevel: undefined,
    regionId: undefined,
    province: undefined,
    city: undefined,
    area: undefined,
    contact: undefined,
    label: undefined,
    status: undefined
  }
})

const { queryParams } = toRefs(data)

const starLevelArr = ['零星', '一星', '二星', '三星', '四星', '五星', '六星', '七星']

const regionsList = computed(() => useUserStore().regionsList)

/** 查询酒店信息列表 */
function getList() {
  loading.value = true

  if (queryParams.value.regionId) {
    queryParams.value.province = queryParams.value.regionId[0]
    queryParams.value.city = queryParams.value.regionId[1]
    queryParams.value.area = queryParams.value.regionId[2]
  } else {
    queryParams.value.province = null
    queryParams.value.city = null
    queryParams.value.area = null
  }

  listHotel(queryParams.value).then(response => {
    hotelList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function handleExpandChange(row) {
  if (!row.rooms) {
    row.loading = true
    listHotelRoom({
      hotelId: row.id
    }).then(res => {
      row.loading = false
      row.rooms = res.rows
    })
  }
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs['modifyHotelRef'].open({
    title: '添加酒店信息'
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  proxy.$modal.loading()
  const _id = row.id || ids.value
  getHotel(_id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyHotelRef'].open({
      title: '修改酒店信息',
      hotel: response.data
    })
  })
}

function handleView(row) {
  proxy.$modal.loading()
  const _id = row.id || ids.value
  getHotel(_id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyHotelRef'].open({
      title: '查看酒店信息',
      hotel: response.data,
      isView: true
    })
  })
}

function handleStatus(row) {
  changeStatus({
    id: row.id,
    status: row.status == '0' ? '1' : '0'
  }).then(getList)
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除"' + row.name + '"的数据项？').then(function() {
    loading.value = true
    return delHotel(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).finally(() => {
    loading.value = false
  })
}


function handleAddRoom(row) {
  proxy.$refs['modifyHotelRoomRef'].open({
    title: '添加酒店房间信息',
    hotelId: row.id
  })
}

function handleUpdateRoom(row) {
  proxy.$modal.loading()
  getHotelRoom(row.id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyHotelRoomRef'].open({
      title: '修改酒店房间信息',
      room: response.data
    })
  })
}

function handleDeleteRoom(hotel, room) {
  proxy.$modal.confirm('是否确认删除酒店房间"' + room.roomType + '"的数据项？').then(function() {
    hotel.loading = true
    return delHotelRoom(room.id)
  }).then(() => {
    listHotelRoom({
      hotelId: hotel.id
    }).then(res => {
      hotel.rooms = res.rows
    })
    proxy.$modal.msgSuccess('删除成功')
  }).finally(() => {
    hotel.loading = false
  })
}

function getRoomList(hotelId) {
  const hotel = _.find(hotelList.value, (h) => {
    return h.id == hotelId
  })
  hotel.loading = true
  listHotelRoom({
    hotelId: hotelId
  }).then(res => {
    hotel.loading = false
    hotel.rooms = res.rows
  })
}

getList()
</script>
